home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / quicktime / quicktime vr / vrbackbuffer / application files / comapplication.c next >
Encoding:
Text File  |  2000-06-23  |  13.8 KB  |  516 lines

  1. //////////
  2. //
  3. //    File:        ComApplication.c
  4. //
  5. //    Contains:    Application-specific code for QuickTime movies demo.
  6. //                This file is used for BOTH MacOS and Windows.
  7. //
  8. //    Written by:    Tim Monroe
  9. //
  10. //    Copyright:    © 1994-1998 by Apple Computer, Inc., all rights reserved.
  11. //
  12. //    Change History (most recent first):
  13. //
  14. //       <11>         10/23/97    rtm        moved InitializeQTVR to InitApplication, TerminateQTVR to StopApplication
  15. //       <10>         10/13/97    rtm        reworked HandleApplicationMenu to use menu identifiers
  16. //       <9>         09/11/97    rtm        merged MacApplication.c and WinApplication.c into ComApplication.c
  17. //       <8>         08/21/97    rtm        first file for Windows; based on MacApplication.c for Mac sample code
  18. //       <7>         06/04/97    rtm        removed call to QTVRUtils_IsQTVRMovie in InitApplicationWindowObject
  19. //       <6>         02/06/97    rtm        fixed window resizing code
  20. //       <5>         12/11/96    rtm        begun QuickTime movie work
  21. //       <4>         12/05/96    rtm        added hooks into MacFramework.c: StopApplication, InitApplicationWindowObject
  22. //       <3>         12/02/96    rtm        added cursor updating to DoIdle
  23. //       <2>         11/27/96    rtm        conversion to personal coding style; added preliminary QTVR support
  24. //       <1>         12/21/94    khs        first file
  25. //       
  26. //////////
  27.  
  28. // header files
  29. #include "ComApplication.h"
  30.  
  31. // header file for the movie functions.
  32. #include "VRBackBuffer.h"
  33.  
  34. // global variables for Macintosh code
  35. #if TARGET_OS_MAC
  36. #endif
  37.  
  38. // global variables for Windows code
  39. #if TARGET_OS_WIN32
  40. extern HWND                ghWnd;                                    // the MDI frame window; this window has the menu bar
  41. extern int                gNumWindowsOpen;
  42. extern LPSTR            gCmdLine;
  43. #endif
  44.  
  45. long                    gMaxMilliSecToUse = 0L;        
  46. Boolean                    gQTVRMgrIsPresent = false;                // is the QuickTime VR Manager available?        
  47. long                    gQTVRMgrVersion = 0L;                    // the version of the QuickTime VR Manager    
  48.  
  49.  
  50. //////////
  51. //
  52. // InitApplication
  53. // Do any application-specific initialization.
  54. //
  55. // The theStartPhase parameter determines which "phase" of application start-up is executed,
  56. // *before* the MDI frame window is created or *after*. This distinction is relevant only on
  57. // Windows, so on MacOS, you should always use kInitAppPhase_BothPhases.
  58. //
  59. //////////
  60.  
  61. void InitApplication (UInt32 theStartPhase)
  62. {
  63.     // ***do any start-up activities that should occur before the MDI frame window is created
  64.     if (theStartPhase & kInitAppPhase_BeforeCreateFrameWindow) {
  65.  
  66.         // make sure that the QuickTime VR Manager is available in the present operating environment;
  67.         // if it is, get its version and (if necessary) initialize it
  68.         if (QTVRUtils_IsQTVRMgrInstalled()) {
  69.             gQTVRMgrIsPresent = true;
  70.             gQTVRMgrVersion = QTVRUtils_GetQTVRVersion();        // get the version of the QuickTime VR Manager
  71.         
  72. #if TARGET_OS_WIN32
  73.             // initialize the QuickTime VR Manager
  74.             InitializeQTVR();                                    
  75. #endif
  76.         }
  77.         
  78.     }    // end of kInitAppPhase_BeforeCreateFrameWindow
  79.  
  80.     // ***do any start-up activities that should occur after the MDI frame window is created
  81.     if (theStartPhase & kInitAppPhase_AfterCreateFrameWindow) {
  82.         
  83. #if TARGET_OS_WIN32
  84.         // on Windows, open as movie documents any files specified on the command line
  85.         DoOpenCommandLineMovies(gCmdLine);                                    
  86. #endif
  87.  
  88.     }    // end of kInitAppPhase_AfterCreateFrameWindow
  89. }
  90.  
  91.  
  92. //////////
  93. //
  94. // StopApplication
  95. // Do any application-specific shut-down.
  96. //
  97. // The theStopPhase parameter determines which "phase" of application shut-down is executed,
  98. // *before* any open movie windows are destroyed or *after*.
  99. //
  100. //////////
  101.  
  102. void StopApplication (UInt32 theStopPhase)
  103. {
  104.     // do any shut-down activities that should occur after the movie windows are destroyed
  105.     if (theStopPhase & kStopAppPhase_AfterDestroyWindows) {
  106.     
  107. #if TARGET_OS_WIN32
  108.         // terminate QuickTime VR Manager
  109.         if (gQTVRMgrIsPresent)
  110.             TerminateQTVR();
  111. #endif
  112.             
  113.     }    // end of kStopAppPhase_AfterDestroyWindows
  114. }
  115.  
  116.  
  117. //////////
  118. //
  119. // DoIdle
  120. // Do any processing that can/should occur at idle time.
  121. //
  122. //////////
  123.  
  124. void DoIdle (WindowReference theWindow)
  125. {
  126.     WindowObject         myWindowObject = NULL;
  127.     GrafPtr             mySavedPort;
  128.     
  129.     GetPort(&mySavedPort);
  130.     MacSetPort(GetPortFromWindowReference(theWindow));
  131.     
  132.     myWindowObject = GetWindowObjectFromWindow(theWindow);
  133.     if (myWindowObject != NULL) {
  134.         MovieController        myMC = NULL;
  135.     
  136.         myMC = (**myWindowObject).fController;
  137.         if (myMC != NULL) {
  138.  
  139. #if TARGET_OS_MAC
  140.             // restore the cursor to the arrow
  141.             // if it's outside the front movie window or outside the window's visible region
  142.             if (theWindow == GetFrontMovieWindow()) {
  143.                 Rect    myRect;
  144.                 Point    myPoint;
  145.                 
  146.                 GetMouse(&myPoint);
  147.                 MCGetControllerBoundsRect(myMC, &myRect);
  148.                 if (!MacPtInRect(myPoint, &myRect) || !PtInRgn(myPoint, GetPortFromWindowReference(theWindow)->visRgn))
  149.                     MacSetCursor(&qd.arrow);
  150.             }
  151. #endif    // TARGET_OS_MAC
  152.  
  153.         }
  154.     }
  155.  
  156.     MacSetPort(mySavedPort);
  157. }
  158.  
  159.  
  160. //////////
  161. //
  162. // DoUpdateWindow
  163. // Update the specified window.
  164. //
  165. //////////
  166.  
  167. void DoUpdateWindow (WindowReference theWindow, Rect *theRefreshArea)
  168. {
  169.     GrafPtr             mySavedPort;
  170.     
  171.     GetPort(&mySavedPort);
  172.     MacSetPort(GetPortFromWindowReference(theWindow));
  173.     
  174.     BeginUpdate(GetPortFromWindowReference(theWindow));
  175.     
  176.     // draw the movie controller and its movie
  177.     MCDoAction(GetMCFromWindow(theWindow), mcActionDraw, theWindow);
  178.     
  179.     EndUpdate(GetPortFromWindowReference(theWindow));
  180.     MacSetPort(mySavedPort);
  181. }
  182.  
  183.  
  184. //////////
  185. //
  186. // HandleContentClick
  187. // Handle mouse button clicks in the specified window.
  188. //
  189. //////////
  190.  
  191. void HandleContentClick (WindowReference theWindow, EventRecord *theEvent)
  192. {
  193.     GrafPtr             mySavedPort;
  194.     
  195.     GetPort(&mySavedPort);
  196.     MacSetPort(GetPortFromWindowReference(theWindow));
  197.     
  198.     // @@@INSERT APPLICATION-SPECIFIC CONTENT CLICKING FUNCTIONALITY HERE
  199.  
  200.     MacSetPort(mySavedPort);
  201. }
  202.  
  203.  
  204. //////////
  205. //
  206. // HandleApplicationKeyPress
  207. // Handle application-specific key presses.
  208. // Returns true if the key press was handled, false otherwise.
  209. //
  210. //////////
  211.  
  212. Boolean HandleApplicationKeyPress (char theCharCode)
  213. {
  214.     Boolean        isHandled = true;
  215.     
  216.     switch (theCharCode) {
  217.     
  218.         // @@@HANDLE APPLICATION-SPECIFIC KEY PRESSES HERE
  219.  
  220.         default:
  221.             isHandled = false;
  222.             break;
  223.     }
  224.  
  225.     return(isHandled);
  226. }
  227.  
  228.  
  229. #if TARGET_OS_MAC
  230. //////////
  231. //
  232. // CreateMovieWindow
  233. // Create a window to display a movie in.
  234. //
  235. //////////
  236.  
  237. WindowRef CreateMovieWindow (Rect *theRect, Str255 theTitle)
  238. {
  239.     WindowRef            myWindow;
  240.         
  241.     myWindow = NewCWindow(NULL, theRect, theTitle, false, noGrowDocProc, (WindowPtr)-1L, true, 0);
  242.     return(myWindow);
  243. }
  244. #endif
  245.  
  246.  
  247. //////////
  248. //
  249. // HandleApplicationMenu
  250. // Handle selections in the application's menus.
  251. //
  252. // The theMenuItem parameter is a UInt16 version of the Windows "menu item identifier". 
  253. // When called from Windows, theMenuItem is simply the menu item identifier passed to the window proc.
  254. // When called from MacOS, theMenuItem is constructed like this:
  255. //     *high-order 8 bits == the Macintosh menu ID (1 thru 256)
  256. //     *low-order 8 bits == the Macintosh menu item (sequential from 1 to ordinal of last menu item in menu)
  257. // In this way, we can simplify the menu-handling code. There are, however, some limitations,
  258. // mainly that the menu item identifiers on Windows must be derived from the Mac values. 
  259. //
  260. //////////
  261.  
  262. void HandleApplicationMenu (UInt16 theMenuItem)
  263. {
  264.     WindowObject            myWindowObject = NULL;
  265.     ApplicationDataHdl        myAppData = NULL;
  266.     MovieController         myMC = NULL;
  267.     
  268.     myWindowObject = GetWindowObjectFromFrontWindow();
  269.     if (myWindowObject != NULL)
  270.         myMC = (**myWindowObject).fController;
  271.     
  272.     // make sure we have a valid movie controller
  273.     if (myMC == NULL)
  274.         return;
  275.     
  276.     switch (theMenuItem) {
  277.     
  278.         case IDM_ROT_PICT_HORIZ_OFF:
  279.             VRBB_InstallBackBufferImagingProc((**myWindowObject).fInstance, myWindowObject, kQTVRRotatedPictID, false);
  280.             break;
  281.             
  282.         case IDM_ROT_PICT_HORIZ_ON:
  283.             VRBB_InstallBackBufferImagingProc((**myWindowObject).fInstance, myWindowObject, kQTVRRotatedPictID, true);
  284.             break;
  285.             
  286.         case IDM_NORM_PICT_HORIZ_ON:
  287.             VRBB_InstallBackBufferImagingProc((**myWindowObject).fInstance, myWindowObject, kQTVRNormalPictID, true);
  288.             break;
  289.             
  290.         case IDM_NORM_PICT_HORIZ_OFF:
  291.             VRBB_InstallBackBufferImagingProc((**myWindowObject).fInstance, myWindowObject, kQTVRNormalPictID, false);
  292.             break;
  293.             
  294.         case IDM_UNINSTALL_BBUF:
  295.             VRBB_CleanUpBackBuffer(myWindowObject);
  296.             break;
  297.             
  298.         case IDM_AM_I_HORIZ:
  299.             if (VRBB_IsBackBufferHorizontal((**myWindowObject).fInstance))
  300. #if TARGET_OS_MAC
  301.                 ShowWarning("\pYes, the back buffer is horizontal.", noErr);
  302. #elif TARGET_OS_WIN32
  303.                 DoCautionAlert(ghWnd, IDS_BBUFDIALOG, MB_ICONEXCLAMATION, MB_OK, "", "Yes, the back buffer is horizontal.");
  304. #endif
  305.             else
  306. #if TARGET_OS_MAC
  307.                 ShowWarning("\pNo, the back buffer is not horizontal.", noErr);
  308. #elif TARGET_OS_WIN32
  309.                 DoCautionAlert(ghWnd, IDS_BBUFDIALOG, MB_ICONEXCLAMATION, MB_OK, "", "No, the back buffer is not horizontal.");
  310. #endif
  311.             break;
  312.  
  313.         default:
  314.             break;
  315.     }    // switch (theMenuItem)
  316.     
  317.     myAppData = (ApplicationDataHdl)GetAppDataFromWindowObject(myWindowObject);
  318.     if (myAppData != NULL)
  319.         if ((theMenuItem >= IDM_ROT_PICT_HORIZ_OFF) && (theMenuItem <= IDM_NORM_PICT_HORIZ_OFF))
  320.             (**myAppData).fCurrMenuItem = theMenuItem;
  321. }
  322.  
  323.  
  324. //////////
  325. //
  326. // AdjustApplicationMenus
  327. // Adjust state of items in the application's menus.
  328. //
  329. // Currently, the Mac application has only one app-specific menu ("Test"); you could change that.
  330. //
  331. //////////
  332.  
  333. void AdjustApplicationMenus (WindowReference theWindow, MenuReference theMenu)
  334. {
  335.     WindowObject            myWindowObject = NULL;
  336.     ApplicationDataHdl        myAppData = NULL;
  337.     MenuReference            myMenu;
  338.     Boolean                    myIsValidVRMovie = false;
  339.     Boolean                    myIsPanoNode = false;
  340.     UInt16                    myIndex;
  341.     
  342. #if TARGET_OS_WIN32
  343.     myMenu = theMenu;
  344. #elif TARGET_OS_MAC
  345.     myMenu = GetMenuHandle(kTestMenu);
  346. #endif
  347.     
  348.     myWindowObject = (WindowObject)GetWindowObjectFromFrontWindow();
  349.     if (myWindowObject != NULL) {
  350.         myIsValidVRMovie = ((**myWindowObject).fInstance != NULL);
  351.         if (myIsValidVRMovie)
  352.             myIsPanoNode = (QTVRGetNodeType((**myWindowObject).fInstance, 0) == kQTVRPanoramaType);
  353.     }    
  354.     
  355.     SetMenuItemState(myMenu, IDM_ROT_PICT_HORIZ_OFF, myIsPanoNode ? kEnableMenuItem : kDisableMenuItem);
  356.     SetMenuItemState(myMenu, IDM_ROT_PICT_HORIZ_ON, myIsPanoNode ? kEnableMenuItem : kDisableMenuItem);
  357.     SetMenuItemState(myMenu, IDM_NORM_PICT_HORIZ_ON, myIsPanoNode ? kEnableMenuItem : kDisableMenuItem);
  358.     SetMenuItemState(myMenu, IDM_NORM_PICT_HORIZ_OFF, myIsPanoNode ? kEnableMenuItem : kDisableMenuItem);
  359.     SetMenuItemState(myMenu, IDM_UNINSTALL_BBUF, myIsPanoNode ? kEnableMenuItem : kDisableMenuItem);
  360.     SetMenuItemState(myMenu, IDM_AM_I_HORIZ, myIsPanoNode ? kEnableMenuItem : kDisableMenuItem);
  361.     
  362.     myAppData = (ApplicationDataHdl)GetAppDataFromWindowObject(myWindowObject);
  363.     if (myAppData == NULL)
  364.         return;
  365.  
  366.     for (myIndex = IDM_ROT_PICT_HORIZ_OFF; myIndex <= IDM_NORM_PICT_HORIZ_OFF; myIndex++)
  367.         SetMenuItemCheck(myMenu, myIndex, myIndex == (**myAppData).fCurrMenuItem);
  368. }
  369.  
  370.  
  371. //////////
  372. //
  373. // DoApplicationEventLoopAction
  374. // Perform any application-specific event loop actions.
  375. //
  376. // Return true to indicate that we've completely handled the event here, false otherwise.
  377. //
  378. //////////
  379.  
  380. Boolean DoApplicationEventLoopAction (EventRecord *theEvent)
  381. {
  382.     return(false);            // no-op for now
  383. }
  384.  
  385.  
  386. //////////
  387. //
  388. // AddControllerFunctionality
  389. // Configure the movie controller.
  390. //
  391. //////////
  392.  
  393. void AddControllerFunctionality (MovieController theMC)
  394. {
  395.     long            myControllerFlags;
  396.     
  397.     // CLUT table use    
  398.     MCDoAction(theMC, mcActionGetFlags, &myControllerFlags);
  399.     MCDoAction(theMC, mcActionSetFlags, (void *)(myControllerFlags | mcFlagsUseWindowPalette));
  400.  
  401.     // enable keyboard event handling    
  402.     MCDoAction(theMC, mcActionSetKeysEnabled, (void *)true);
  403.     
  404.     // disable drag support
  405.     MCDoAction(theMC, mcActionSetDragEnabled, (void *)false);
  406. }
  407.  
  408.  
  409. //////////
  410. //
  411. // InitApplicationWindowObject
  412. // Do any application-specific initialization of the window object.
  413. //
  414. //////////
  415.  
  416. void InitApplicationWindowObject (WindowObject theWindowObject)
  417. {
  418.     Track                    myQTVRTrack = NULL;
  419.     Movie                    myMovie = NULL;
  420.     MovieController            myMC = NULL;
  421.     QTVRInstance            myInstance = NULL;
  422.     ApplicationDataHdl        myAppData;
  423.         
  424.     if (theWindowObject == NULL)
  425.         return;
  426.  
  427.     // make sure we can safely call the QTVR API
  428.     if (!gQTVRMgrIsPresent)
  429.         return;
  430.  
  431.     // find the QTVR track, if there is one
  432.     myMC = (**theWindowObject).fController;
  433.     myMovie = (**theWindowObject).fMovie;
  434.     myQTVRTrack = QTVRGetQTVRTrack(myMovie, 1);
  435.     
  436.     QTVRGetQTVRInstance(&myInstance, myQTVRTrack, myMC);
  437.     (**theWindowObject).fInstance = myInstance;
  438.  
  439.     // do any QTVR window configuration
  440.     if (myInstance != NULL) {
  441.  
  442.         // set unit to radians
  443.         QTVRSetAngularUnits(myInstance, kQTVRRadians);
  444.  
  445.         // do QuickTime window configuration
  446.         myAppData = VRBB_InitWindowData(theWindowObject);
  447.         (**theWindowObject).fAppData = (Handle)myAppData;
  448.     }
  449. }
  450.  
  451.  
  452. //////////
  453. //
  454. // RemoveApplicationWindowObject
  455. // Do any application-specific clean-up of the window object.
  456. //
  457. //////////
  458.  
  459. void RemoveApplicationWindowObject (WindowObject theWindowObject)
  460. {
  461.     OSErr                myErr = noErr;
  462.     QTVRInstance        myInstance = NULL;
  463.     ApplicationDataHdl    myAppData;
  464.         
  465.     // application clean-up
  466.     myAppData = (ApplicationDataHdl)GetAppDataFromWindowObject(theWindowObject);
  467.     if (myAppData != NULL) {
  468.         VRBB_DumpWindowData(theWindowObject);
  469.         DisposeHandle((Handle)myAppData);
  470.         (**theWindowObject).fAppData = NULL;
  471.     }
  472.  
  473.     // DoDestroyMovieWindow in MacFramework.c or MovieWndProc in WinFramework.c
  474.     // releases the window object itself
  475. }
  476.  
  477.  
  478. //////////
  479. //
  480. // ApplicationMCActionFilterProc 
  481. // Intercept some mc actions for the movie controller.
  482. //
  483. // NOTE: The theRefCon parameter is a handle to a window object record.
  484. //
  485. //////////
  486.  
  487. PASCAL_RTN Boolean ApplicationMCActionFilterProc (MovieController theMC, short theAction, void *theParams, long theRefCon)
  488. {
  489. #pragma unused(theMC, theParams)
  490.  
  491.     Boolean                isHandled = false;
  492.     WindowObject        myWindowObject = NULL;
  493.     
  494.     myWindowObject = (WindowObject)theRefCon;
  495.     if (myWindowObject == NULL)
  496.         return(isHandled);
  497.         
  498.     switch (theAction) {
  499.     
  500.         // handle window resizing
  501.         case mcActionControllerSizeChanged:
  502.             SizeWindowToMovie(myWindowObject);
  503.             break;
  504.  
  505.         // handle idle events
  506.         case mcActionIdle:
  507.             DoIdle((**myWindowObject).fWindow);
  508.             break;
  509.             
  510.         default:
  511.             break;
  512.             
  513.     }    // switch (theAction)
  514.     
  515.     return(isHandled);    
  516. }